import pickle
import requests
import json


def login():
    session = requests.session()
    url = "https://lagou.com/index/job?text&jks"
    username = "yaosheng"
    password = "YaoSheng0216"
    headers = {
        "Referer": "https://www.lagou.com/",
        "User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36"
    }

    post_data = {
        "ck": "",
        "name": username,
        "password": password,
        "remember": "true",
        "tickect": ""
    }
    res = requests.post(url, data=post_data,headers=headers)
    res_json = json.loads(res.text)

    if res_json["status"] == "success":
        print("登陆成功")
        with open("loagou.cookie", "wb") as f:
            pickle.dump(res.cookies, f)
    else:
        print("登陆失败")

    with open("lagou.cookie", "rb") as f:
        cookies = pickle.load(f)
        html = requests.get("http://www.lagou.com/", cookies=cookies).text
        if "yaosheng" in html:
            print("已登陆")
        else:
            print("未登录")

if __name__ == '__main__':
    login()